package genalgoritm.editors;

import genalgoritm.count.Function;
import genalgoritm.count.RealChromosome;
import genalgoritm.viewer.PersonContentProvider;
import genalgoritm.viewer.PersonLabelProvider;
import genalgoritm.viewer.TableSorter;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.general.DatasetGroup;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYIntervalSeries;
import org.jfree.data.xy.XYSeries;
import org.jfree.experimental.chart.swt.ChartComposite;
import org.jfree.ui.RectangleInsets;

public class ResultComposite extends Composite {
	private TableViewer pointsViewer;
	private TableSorter tableSorter;
	private static double[][] functionData = Function.getFunction();

	public ResultComposite(FormToolkit toolkit, Composite parent, int style,
			ArrayList<RealChromosome> result) {
		super(parent, style);
		this.setLayout(new GridLayout());
		this.setLayoutData(new GridData(GridData.FILL_BOTH));

		Composite mainComposite = toolkit.createComposite(this);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		mainComposite.setLayout(layout);
		mainComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
		//		
		Composite tableComposite = toolkit.createComposite(mainComposite);
		tableComposite.setLayout(new GridLayout());
		tableComposite.setLayoutData(new GridData());
		createViewer(tableComposite);
		pointsViewer.setInput(result);
		// Set the sorter for the table
		tableSorter = new TableSorter();
		pointsViewer.setSorter(tableSorter);

		Composite chartComposite = toolkit.createComposite(mainComposite);
		chartComposite.setLayout(new GridLayout());
		chartComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
		JFreeChart chart = createChart(createDataset(result));
		final ChartComposite frame = new ChartComposite(chartComposite,
				SWT.NONE, chart, true);
		GridData layoutData = new GridData(GridData.FILL_BOTH);
		layoutData.heightHint = 300;
		frame.setLayoutData(layoutData);

	}

	private XYDataset createDataset(ArrayList<RealChromosome> result) {
		DefaultXYDataset dataset = new DefaultXYDataset();
		
		double[][] pointsData = new double[2][result.size()];
		for (int j = 0; j<result.size(); j++) {
			pointsData[0][j] = result.get(j).getX();// x
			pointsData[1][j] = result.get(j).fitness();// y	
		}
		dataset.addSeries(0.0, pointsData);

		dataset.addSeries(1.0, functionData);

		
		return dataset;
	}

	private JFreeChart createChart(XYDataset dataset) {

		JFreeChart chart = ChartFactory.createXYLineChart("", "x", "f(x)",
				dataset, PlotOrientation.VERTICAL, false, false, false);

		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setDomainGridlinesVisible(true);
		plot.setNoDataMessage("No data available");
		plot.setBackgroundPaint(Color.white);
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDomainGridlinePaint(Color.gray);
		plot.setRangeGridlinePaint(Color.lightGray);

		XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot
				.getRenderer();
		renderer.setBaseShapesVisible(true);
		renderer.setBaseShapesFilled(true);
		renderer.setSeriesLinesVisible(0, false);

		return chart;

	}

	private void createViewer(Composite parent) {
		pointsViewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.FULL_SELECTION);
		createColumns(pointsViewer);
		pointsViewer.setContentProvider(new PersonContentProvider());
		pointsViewer.setLabelProvider(new PersonLabelProvider());
	}

	private void createColumns(final TableViewer viewer) {
		for (int i = 0; i < PersonLabelProvider.COLS.length; i++) {
			final int index = i;
			final TableViewerColumn column = new TableViewerColumn(viewer,
					SWT.NONE);
			column.getColumn().setText(PersonLabelProvider.COLS[i]);
			column.getColumn().setWidth(PersonLabelProvider.WIDTHS[i]);
			column.getColumn().setResizable(true);
			column.getColumn().setMoveable(true);
			// Setting the right sorter
			column.getColumn().addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					tableSorter.setColumn(index);
					int dir = viewer.getTable().getSortDirection();
					if (viewer.getTable().getSortColumn() == column.getColumn()) {
						dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
					} else {

						dir = SWT.DOWN;
					}
					viewer.getTable().setSortDirection(dir);
					viewer.getTable().setSortColumn(column.getColumn());
					viewer.refresh();
				}
			});
		}
		Table table = viewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setLayoutData(new GridData(GridData.FILL_BOTH));
	}
}
